package Demo523;

import java.sql.*;
import java.util.Scanner;

public class LoginDemo {
    private static final String URL = "jdbc:mysql://localhost:3306/userdb";
    private static final String username = "root";
    private static final String password = "123456";

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection connection = DriverManager.getConnection(URL,username,password);

        String sql = "select * from user where username = ? and password = ?";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();

        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);

        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println("登录成功");
        }
        else {
            System.out.println("登录失败");
        }
    }
}
